package com.blue.consumer;

import com.alibaba.fastjson.JSON;
import com.blue.mapper.DataSyncMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@Slf4j
public class DataSyncConsumer {

    @Autowired
    private DataSyncMapper dataSyncMapper;

    @RabbitListener(queues = "dataSyncQueue")
    public void dateSyncListener(String content) {
        Map map = JSON.parseObject(content, Map.class);
        String deleteSql = (String) map.get("deleteSql");
//        log.info("获取到的deleteSql为:{}", deleteSql);
        String insertSql = (String) map.get("insertSql");
//        log.info("获取到的insertSql为:{}", insertSql);
        try {
            if (deleteSql != null) {
                dataSyncMapper.executeDeleteSql(deleteSql);
            }
            if (insertSql != null) {
                dataSyncMapper.executeInsertSql(insertSql);
            }
        } catch (Exception e) {
            log.info("数据同步消费失败了,异常:{},deleteSql为:{},insertSql为:{}", e.getMessage(), deleteSql, insertSql);
            String confId = (String) map.get("confId");
            dataSyncMapper.recordFail(deleteSql, insertSql, confId);
        }
    }
}
